﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;

namespace PDOGenerator.Util
{
    public enum MessageDialogResults
    {
        None = 0,
        OK = 1,
        Cancel = 2,
        Yes = 3,
        No = 4,
    }

    public enum MessageDialogIcons
    {
        None = 0,
        Information = 1,
        Question = 2,
        Shield = 3,
        Stop = 4,
        Warning = 5,
    }

    public enum MessageDialogButtons
    {
        OK = 0,
        OKCancel = 1,
        YesNo = 2,
        YesNoCancel = 3,
    }

    /// <summary>
    /// Interaction logic for MessageDialog.xaml
    /// </summary>
    public partial class MessageDialog : Window
    {
        public MessageDialog()
        {
            InitializeComponent();
        }

        public static void ShowMessage(string header, string message)
        {
            MessageDialog dlg = new MessageDialog();
            dlg.Caption = "PDO Generation";
            dlg.Header = header;
            dlg.Message = message;
            dlg.MessageIcon = MessageDialogIcons.Information;
            dlg.ShowDialog();
        }

        public static void ShowMessage(string header, string message, string exceptionDetails)
        {
            MessageDialog dlg = new MessageDialog();
            dlg.Caption = "PDO Generation";
            dlg.Header = header;
            dlg.Message = message;
            dlg.DetailsText = exceptionDetails;
            dlg.MessageIcon = MessageDialogIcons.Information;
            dlg.ShowDialog();
        }


        private MessageDialogResults _dialogResult;
        public MessageDialogResults MessageDialogResult
        {
            get { return _dialogResult; }
        }

        public string Caption
        {
            get { return this.Title; }
            set { this.Title = value; }
        }

        public string Header
        {
            get { return _headerTextBox.Text; }
            set { _headerTextBox.Text = value; }
        }

        public string Message
        {
            get { return _messageTextBox.Text; }
            set { _messageTextBox.Text = value; }
        }

        public string DetailsText
        {
            get { return _detailText.Text; }
            set
            {
                _detailText.Text = value;

                if (null != value)
                {
                    this.Height = 200;
                }
            }
        }

        public string FooterText
        {
            get { return _footerTextBox.Text; }
            set { _footerTextBox.Text = value; }
        }

        private MessageDialogIcons _messageIcon;

        private BitmapImage IconImage(MessageDialogIcons icon)
        {
            BitmapImage img = null;
            string iconFileName = null;
            switch (icon)
            {
                case MessageDialogIcons.Information:
                    iconFileName = "MessageDialogInformation.png";
                    break;

                case MessageDialogIcons.None:
                    iconFileName = null;
                    break;

                case MessageDialogIcons.Question:
                    iconFileName = "MessageDialogQuestion.png";
                    break;

                case MessageDialogIcons.Shield:
                    iconFileName = "MessageDialogShield.png";
                    break;

                case MessageDialogIcons.Stop:
                    iconFileName = "MessageDialogStop.png";
                    break;

                case MessageDialogIcons.Warning:
                    iconFileName = "MessageDialogWarning.png";
                    break;
            }

            if (null != iconFileName)
            {
                img = new BitmapImage(new Uri("pack://application:,,,/Icons/" + iconFileName));
            }
            return img;
        }

        public MessageDialogIcons MessageIcon
        {
            get { return _messageIcon; }
            set
            {
                _messageIcon = value;
                _messageImage.Source = IconImage(value);
            }
        }
        private MessageDialogIcons _footerIcon;

        public MessageDialogIcons FooterIcon
        {
            get { return _footerIcon; }
            set
            {
                _footerIcon = value;
                _footerImage.Source = IconImage(value);
            }
        }

        private MessageDialogResults _defaultButtonValue;
        private MessageDialogResults _otherButtonValue;

        private MessageDialogButtons _buttons;

        public MessageDialogButtons Buttons
        {
            get { return _buttons; }

            set
            {
                _buttons = value;
                switch (_buttons)
                {
                    case MessageDialogButtons.OK:
                        _otherBtn.Visibility = Visibility.Hidden;
                        _closeBtn.Visibility = Visibility.Visible;
                        _closeBtn.Content = "OK";
                        _defaultButtonValue = MessageDialogResults.OK;
                        break;

                    case MessageDialogButtons.OKCancel:
                        _otherBtn.Visibility = Visibility.Visible;
                        _closeBtn.Visibility = Visibility.Visible;
                        _closeBtn.Content = "OK";
                        _otherBtn.Content = "Cancel";
                        _defaultButtonValue = MessageDialogResults.OK;
                        _otherButtonValue = MessageDialogResults.Cancel;
                        break;

                    case MessageDialogButtons.YesNo:
                        _otherBtn.Visibility = Visibility.Visible;
                        _closeBtn.Visibility = Visibility.Visible;
                        _closeBtn.Content = "Yes";
                        _otherBtn.Content = "No";
                        _defaultButtonValue = MessageDialogResults.Yes;
                        _otherButtonValue = MessageDialogResults.No;
                        break;
                }
            }
        }

        public MessageDialogResults DefaultButton
        {
            get { return _defaultButtonValue; }
            set { _defaultButtonValue = value; }
        }

        private void _closeBtn_Click(object sender, RoutedEventArgs e)
        {
            _dialogResult = _defaultButtonValue;
            this.Close();
        }

        private void _otherBtn_Click(object sender, RoutedEventArgs e)
        {
            _dialogResult = _otherButtonValue;
            this.Close();
        }

        private void _detailExpander_Expanded(object sender, RoutedEventArgs e)
        {
            this.ResizeMode = ResizeMode.CanResize;
            this.Height = 450;
            _detailText.Visibility = Visibility.Visible;
        }

        private void _detailExpander_Collapsed(object sender, RoutedEventArgs e)
        {
            this.ResizeMode = ResizeMode.NoResize;
            this.Height = 200;
            _detailText.Visibility = Visibility.Collapsed;
        }

    }
}
